<?php
/**
 * Created by PhpStorm.
 * User: Silence
 * Date: 2017/9/21
 * Time: 18:58
 */

namespace App\Http\Controllers\Business;

use App\Base\BaseFunc;
use App\Base\OrderStatus;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Input;


class ShopController extends Controller
{
    public function myGoods()
    {
        $data['drugData'] = DB::table('re_drug_business')
            ->where('re_business_id','=',session('business_id'))
            ->leftJoin('drug','drug_id','=','re_drug_id')
            ->leftJoin('business','business_id','=','re_business_id')
            ->paginate(5);
        //dd($data['goodsClass']);
        session(['now_address'=>'/myGoods']);
        return view('/business.goodsSearch',$data);
    }

    public function addGoods(BaseFunc $baseFunc)
    {
        $input = Input::only('drug_name','drug_info','drug_symptom');
        $input['drug_time'] = date('Y-m-d H:i:s');
        $data = DB::table('drug')->insert($input);
        if($data)
        {
            $drugData = DB::table('drug')
                ->orderby('drug_id','desc')
                ->first();
            $value['re_business_id'] = session('business_id');
            $value['re_drug_id'] = $drugData->drug_id;
            //dd($value);
            DB::table('re_drug_business')->insert($value);
            $baseFunc->setRedirectMessage(true,'上架成功',NULL,'/myGoods');
        }
    }

    public function delGoods(BaseFunc $baseFunc)
    {
        $input['drug_id'] = Input::only('drug_id');
        DB::table('drug')
            ->where('drug_id','=',$input['drug_id'])
            ->delete();
        $baseFunc->setRedirectMessage(true,'下架成功',NULL,'/myGoods');
    }

    public function upGoods(Request $request,BaseFunc $baseFunc)
    {
        $input['goods_id'] = Input::only('goods_id');

        $file = $request->file('files');
        //dd($file);
        $file_name = $file->getClientOriginalName();
        $destinationPath = public_path() . '/images/';
        $file->move($destinationPath, md5($file_name));

        $goods_name = $request->input('goods_name');
        $goods_price = $request->input('goods_price');
        $goods_info = $request->input('goods_info');
        $goods_time = date('Y-m-d H:i:s');
        $goods_business = session('business_id');
        $image = '/images/' . md5($file_name);
        //dd($goods_name);
        try {
            DB::table('goods')
                ->where('goods_id','=',$input['goods_id'])
                ->insertGetId([
                "goods_image" => $image,
                "goods_price" => $goods_price,
                "goods_info" => $goods_info,
                "goods_name" => $goods_name,
                "goods_time" => $goods_time,
                "goods_business" => $goods_business
            ]);
            $baseFunc->setRedirectMessage(true,'商品上架成功',NULL,NULL);
            return redirect('/myGoods');
        } catch (\Exception $e) {
            return response()->json([
                'message' => $e->getMessage()
            ]);
        }
    }

    public function moreGoods($goods_id)
    {
        $data['goodsData'] = DB::table('goods')
            ->where('goods_id',$goods_id)
            ->leftJoin('goods_class','goods_class_id','=','goods_class')
            ->first();
        //dump($data);
        $data['evaluateData'] = DB::table('evaluate')
            ->where('evaluate_goods','=',$goods_id)
            ->leftJoin('goods','goods_id','=','evaluate_goods')
            ->leftJoin('user','user_id','=','evaluate_user')
            ->get();

        return view('/business.moreGoods',$data);
    }

    public function getOrder()
    {
        $data['orderData'] = DB::table('re_user_drug')
            ->where('re_business','=',session('business_id'))
            ->join('user','user_id','=','re_user_id')
            ->Join('drug','drug_id','=','re_drug_id')
            ->leftJoin('status','status_id','=','re_user_drug_status')
            ->get();
        //dd($data);
        session(['now_address'=>'/getOrder']);
        return view('/business.getOrder',$data);
    }

    public function sendOrder($drug_id,OrderStatus $orderStatus,BaseFunc $baseFunc)
    {
        $input['re_user_drug_status'] = $orderStatus->Ordering()->status_id;
        //dd($input);
        DB::table('re_user_drug')
            ->where('re_drug_id',$drug_id)
            ->update($input);
        $baseFunc->setRedirectMessage(true,'提醒成功！',NULL,'/getOrder');
    }

    public function end($drug_id,OrderStatus $orderStatus,BaseFunc $baseFunc)
    {
        $input['re_user_drug_status'] = $orderStatus->end()->status_id;
        //dd($input);
        DB::table('re_user_drug')
            ->where('re_drug_id',$drug_id)
            ->update($input);
        $baseFunc->setRedirectMessage(true,'订单完成！',NULL,'/getOrder');
    }

    public function searchMyUser()
    {
        $data['userData'] = DB::table('re_user_business')
            ->join('user','user.user_id','=','re_user_business.user_id')
            ->where('business_id',session('business_id'))
            ->get();
        session(['now_address'=>'/searchMyUser']);
        return view('/business.searchMyUser',$data);
    }
}